using System;
using System.Collections.Generic;
using CLELCore.Assembly;
using CLELCore.Assembly.BlobStreams;
using CLELCore.Assembly.BlobStreams.BlobInfo;

namespace CLELCore.Assembly.Descriptors.ClassDescriptors
{
	public class TypeDefClassDescriptor : ClassDescriptor
	{
		private CLELAssembly _assembly;
		private byte _type;
		private Token _token;

		public Token ClassToken
		{
			get
			{
				return _token.Copy();
			}
		}

		public TypeDefClassDescriptor(CLELAssembly assembly,byte type,Token token) : base(ClassDescriptor.TYPEDEF_CLASSDESCRIPTOR)
		{
			if(assembly == null)
				throw new ArgumentException("CLEL assembly cannot be null");
			if(token == null)
				throw new ArgumentException("Class token cannot be null");
			_assembly = assembly;
			_type = type;
			_token = token;
		}

		public List<MethodDescriptor> Methods
		{
			get
			{
				if(_assembly == null)
					throw new AssemblyDisposedException("Assembly has been disposed and set to null");
				return _assembly.GetMethodDescriptorsForClass(_token);
			}
		}
	
		/*	
		public MethodLocalsBlobInfo Locals
		{
			get
			{
				if(_assembly == null)
					throw new ArgumentException("CLEL assembly cannot be null");
				if(_token == null)
					throw new ArgumentException("Class token cannot be null");
				return _assembly.GetMethodsLocals(_token);
			}
		}
		*/

		//TODO: Call clel to update token
		public void Update(object sender, EventArgs e)
		{
			//Console.WriteLine(_token);
		}
		
		public override ClassDescriptor Copy()
		{
			return (ClassDescriptor)new TypeDefClassDescriptor(_assembly,_type,_token.Copy());
		}
		
		public override byte [] ToBytes()
		{
			int row = _token.Row;
			byte [] com = Blob.CompressBlobValue(row<<2); //OR IN TYPEDEF MAGIC NUMBER
			byte [] ret = new byte[com.Length+1];
			//ret[0] = BlobElementType.ELEMENT_TYPE_CLASS;
			ret[0] = _type;
			int i;
			for(i = 0;i < com.Length;i++)
				ret[i+1] = com[i];
			return ret;
		}
	}
}
